package com.service.impl;

import com.service.EsInitService;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.stereotype.Service;

import java.util.HashMap;

import static core.Constants.Es.INDEX_NAME;

/**
 *
 */
@Service
public class EsInitServiceImpl implements EsInitService {
    private ElasticsearchRestTemplate esTemplate;

    public EsInitServiceImpl(ElasticsearchRestTemplate esTemplate) {
        this.esTemplate = esTemplate;
    }

    @Override
    public void init(String indexName) {
        String mappingJson = "{\n" +
                "  \"properties\": {\n" +
                "    \"id\": {\n" +
                "      \"type\": \"keyword\"\n" +
                "    },\n" +
                "    \"name\": {\n" +
                "      \"type\": \"keyword\"\n" +
                "    },\n" +
                "    \"price\": {\n" +
                "      \"type\": \"double\"\n" +
                "    },\n" +
                "    \"productDesc\": {\n" +
                "      \"type\": \"text\",\n" +
                "      \"analyzer\": \"ik_max_word\"\n" +
                "    }\n" +
                "  }\n" +
                "}";
        esTemplate.indexOps(IndexCoordinates.of(INDEX_NAME))
                .create(new HashMap<>(), Document.parse(mappingJson));

        // createIndex(indexName);
        // createMapping(indexName);
    }

    /*
    public void createIndex(String indexName) {

        try (RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http")))) {
            CreateIndexRequest request = new CreateIndexRequest(indexName);
            CreateIndexResponse response = client
                    .indices()
                    .create(request,
                            RequestOptions.DEFAULT);
            boolean acknowledged = response.isAcknowledged();
            System.out.println(acknowledged);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/

  /*  public void createMapping(String indexName) {
        try (RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http")))) {
            PutMappingRequest request = new PutMappingRequest(indexName);
            request.source("{\n" +
                    "  \"properties\": {\n" +
                    "    \"id\": {\n" +
                    "      \"type\": \"keyword\"\n" +
                    "    },\n" +
                    "    \"name\": {\n" +
                    "      \"type\": \"keyword\"\n" +
                    "    },\n" +
                    "    \"price\": {\n" +
                    "      \"type\": \"double\"\n" +
                    "    },\n" +
                    "    \"productDesc\": {\n" +
                    "      \"type\": \"text\",\n" +
                    "      \"analyzer\": \"ik_max_word\"\n" +
                    "    }\n" +
                    "  }\n" +
                    "}", XContentType.JSON);
            AcknowledgedResponse response = client.indices().putMapping(request,
                    RequestOptions.DEFAULT);
            boolean acknowledged = response.isAcknowledged();
            System.out.println(acknowledged);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/
}
